package com.aliptori.fontinstaller

import android.content.Intent
import android.graphics.drawable.GradientDrawable
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.PopupMenu
import android.support.v7.widget.RecyclerView
import android.view.ContextMenu
import android.view.View
import com.aliptori.fontinstaller.Logic.LocalFontManager
import com.ug_project.adapters.createRecyclerAdapter
import com.ug_project.objects.toArrayList
import com.ug_project.views.click
import com.ug_project.views.findTextView
import kotlinx.android.synthetic.main.activity_main_page.*


class MainPage : AppCompatActivity() {
    val localManager: LocalFontManager by lazy { LocalFontManager(this) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main_page)

        containerView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LOW_PROFILE or
                        View.SYSTEM_UI_FLAG_FULLSCREEN or
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION


        Menu.click {
            val menu = PopupMenu(this, it)
            menu.menuInflater.inflate(R.menu.menu_main, menu.menu)

            menu.setOnMenuItemClickListener {
                when(it.itemId){
                    R.id.action_help -> startActivity(Intent(this, HelpActivity::class.java))
                }
                false
            }

            menu.show()
        }



        FontList.layoutManager = LinearLayoutManager(this)
        val data = localManager.localFonts.toArrayList()

        FontList.adapter = createRecyclerAdapter(this, R.layout.font_list_item, data) {
            OnBindViewHolderIData { rootView, itemViewResID, data, position ->
                rootView.findTextView(R.id.Name).text = "${getString(R.string.font_name)}${data.titles.zh_CN}"
                rootView.findTextView(R.id.Designer).text = "${getString(R.string.designer)}${data.authors.zh_CN}"

                rootView.click {
                    startActivity(Intent(this.context, FontDetail::class.java).apply {
                        putExtra(LocalFontManager.fontLocalID, data.localId)
                    })
                }
            }
        }
    }

}
